package code.main
{
	import code.main.core.mvc.YController;
	import code.main.core.net.NetManager;
	import code.main.core.net.YNet;
	import code.structure.manager.assist.ProjectAssistManager;
	import code.structure.manager.background.BackGroundManager;
	import code.structure.manager.font.FontManager;
	import code.structure.manager.language.LanguageManager;
	import code.structure.manager.loader.DllLoaderManager;
	import code.structure.manager.mouse.MouseManager;
	import code.structure.manager.others.SharedObjectManager;
	import code.structure.manager.others.StatisticsManager;
	import code.structure.module.configs.ConfigModel;
	import code.tools.bitmap.FrameDrive;
	import code.tools.other.SecondDrive;
	import code.untils.CommandNameConst;
	import code.untils.LogConst;
	import code.untils.NetConst;
	import code.untils.NetDataBackKeyConst;
	
	import com.patience.mvc.context.Command;
	import com.patience.net.events.NetEvent;
	
	import flash.events.Event;
	import flash.utils.ByteArray;
	import flash.utils.getTimer;
	
	/**
	 * 进游戏所需要做的一个控制器 
	 * @author Yuri
	 * 
	 */
	public class AppController extends YController
	{
		private var _baseInitOver:Boolean;
		
		public function AppController()
		{
			super();
		}
		
		/**
		 * 启动流程 
		 * 
		 */
		override public function start():void{
			startToken();
			
			//直接跳过测试
//			startSetting();
		}
		
		/**
		 *启动Token获取 
		 * 
		 */
		private function startToken():void{
			YNet.getIntance().addEventListener(NetDataBackKeyConst.GET_TOKEN, onCompleteHandler);
			NetManager.getInstance().addNetCmdDetail("index", NetConst.FUN_GETTOKEN, [], true);
			 function onCompleteHandler(event:NetEvent):void
			{
				YNet.getIntance().removeEventListener(NetDataBackKeyConst.GET_TOKEN, onCompleteHandler);
				AppData.token = event.data as String;
				startSetting();
			}
		}
		
		/**
		 *启动配置设置 
		 * 
		 */
		private function startSetting():void{
			BackGroundManager.bgPath = AppData.fileUrl + BackGroundManager.bgPath;
			FrameDrive.stage = App.appstage;
			SecondDrive.stage = App.appstage;
			
			FontManager.getInstance().start();
			SharedObjectManager.getIntance().analyerSharedGameInfo();
			MouseManager.getInstance().init();
			BackGroundManager.getInstance().parserData(JSON.parse(DllLoaderManager.getInstance().getSyncMaterial("gamebg","","json")));
			startLanuage();
			startConfig();
		}
		
		/**
		 *启动多语言解析 
		 * 
		 */
		private function startLanuage():void{
				LanguageManager.xmlLanuage = DllLoaderManager.getInstance().getSyncMaterial("language", "", "xml");
		}
		/**
		 * 启动配置表解析 
		 * 
		 */
		private function startConfig():void{
			//to do 开始配置表解析
			var obj:Object;
			var b:ByteArray = DllLoaderManager.getInstance().getSyncMaterial("outini", "", "ddd") as ByteArray;
			if(b is ByteArray){
				b.uncompress(); 
				obj = JSON.parse(b.toString());
			}
			if(!obj){  //配置表信息出错
				return;
			}
			(_context.commonData as AppCommonData).configModel = new ConfigModel();
			(_context.commonData as AppCommonData).configModel.analySystemData(obj);
			NetManager.getInstance().net.dispatchEvent( new Event("config_over"));
			startBaseInit();			
		}
		
		
		/**
		 *启动初始化后端数据 
		 * 
		 */
		private function startBaseInit():void{
			AppData.iDelayTime = getTimer();
			AppControllerList.getInstance().start();
			NetManager.IS_REQUEST_FIRST_BASE_INIT = true;
			
			
			YNet.getIntance().addEventListener(NetDataBackKeyConst.BASE_INIT_FIRST, getBaseInitInfo);
			//不连后台跳过
			NetManager.getInstance().addNetCmdDetail(NetConst.CLASS_BASE, NetConst.FUN_INIT, [], true);
//			enterGame();
			
		}
		
		
		/**
		 *首次baseinit数据 
		 */
		private var _firstBaseInitData:Object;
		/**
		 *从后端获取到baseinit信息 
		 * @param d
		 * 
		 */
		private function getBaseInitInfo(e:NetEvent):void{
			NetManager.IS_REQUEST_FIRST_BASE_INIT = false;
			StatisticsManager.writeLog(LogConst.PHP_FIRST_INIT_GET);
			_firstBaseInitData = e.data;
			
			if(!_firstBaseInitData){
				trace("问题！");
				return;
			}
			for(var i:int;i<NetDataBackKeyConst.BASEINITCONST.length;i++){
				if(_firstBaseInitData.hasOwnProperty(NetDataBackKeyConst.BASEINITCONST[i])){
					YNet.getIntance().dispatchEvent(new NetEvent(NetDataBackKeyConst.BASEINITCONST[i],_firstBaseInitData[NetDataBackKeyConst.BASEINITCONST[i]]));
				}
			}
			enterGame();
		}
		
		/**
		 * 启动初始化扩展数据（活动数据等）
		 * 
		 */
		private function startBaseExtendsInit():void{
			//请求扩展信息
			NetManager.getInstance().addNetCmdDetail(NetConst.CLASS_BASE,NetConst.FUN_EXTENDS_INIT,[],true);
		}
		
		/**
		 *进入游戏 
		 * 
		 */
		private function enterGame():void{
			//base.ini完毕真正进入游戏
//			startBaseExtendsInit();
			NetManager.getInstance().net.dispatchEvent( new Event("enter_game"));
			ProjectAssistManager.getIntance().startFeedRequest();
			dispatch(new Command(CommandNameConst.COMMAND_ENTER_GAME));
		}
	}
}